<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\ProductModel;
use think\facade\Session;
use think\facade\Db;
class Product extends AdminBase
{
    protected ProductModel $model;

    /**
     * 初始化
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->model = new ProductModel();
    }

    /**
     * 商品管理列表
     */
    public function index()
    {
        $param = $this->request->param();
        $model = $this->model;
        //构造搜索条件
        if( $param['name']!='' ) {
            $model = $model->whereLike('name',"%".$param['name']."%");
        }
        if( $param['number']!='' ) {
            $model = $model->whereLike('number',"%".$param['number']."%");
        }
        if( $param['price']!='' ) {
            $model = $model->whereLike('price',"%".$param['price']."%");
        }
        if( $param['status']!='' ) {
            $model = $model->where('status',$param['status']);
        }
        if( $param['start_date']!=''&&$param['end_date']!='' ) {
            $start_date = $param['start_date'].' 00:00:00';
            $end_date = $param['end_date'].' 23:59:59';
            $model = $model->whereBetweenTime('create_time',$start_date,$end_date);
        }
		$admin=Session::get('admin_auth');
		$info=Db::name('admin')->where(['id'=>$admin['id']])->find();
		if($admin){
			$result=Db::name('auth_group_access')->where(['admin_id'=>$admin['id']])->find();
			// 超级管理员显示全部
			if($result['group_id']==1){
				$list = $model->order('id desc')
					->alias('a')
					->join('shoptype b','a.typeid=b.id')
					->join('shopsubject c','a.subject_id=c.id')
					->join('shopcategory d','a.category_id=d.id')
					->field('a.*,b.name as type,c.title as subject,d.title as category')
				    ->paginate([
				        'query' => $param,
				        'list_rows'=>$this->request->param('limit',20)
				    ]);
			}else{
				// echo 1;exit;
				// 显示各自平台产品
				$list = $model->order('id desc')
				  ->alias('a')
				  ->join('shoptype b','a.typeid=b.id')
				  ->join('shopsubject c','a.subject_id=c.id')
				  ->join('shopcategory d','a.category_id=d.id')
				  ->field('a.*,b.name as type,c.title as subject,d.title as category')
				   ->where('a.platform_id',$info['type_id'])
				    ->paginate([
				        'query' => $param,
				        'list_rows'=>$this->request->param('limit',20)
				    ]);
			}
		}
        return view('', compact('list'));
    }

    /**
     * 添加商品管理
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
			$admin=Session::get('admin_auth');
			
			// 查找各自平台
			$info=Db::name('admin')->where(['id'=>$admin['id']])->find();		
			if($info['user_type']==1){
				$param['platform_id']=$info['type_id'];
			}else{
				// 预留板块
				$param['platform_id	']=0;
			}
            $result = $this->model->save($param);
            if( $result ) {
                xn_add_admin_log('添加商品管理');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
            $result = $this->model->save($param);
            if( $result ) {
                xn_add_admin_log('添加商品管理');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
		$type=Db::name('shoptype')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		$category=Db::name('shopcategory')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		$subject=Db::name('shopsubject')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
        return view('form',compact('type','category','subject'));
    }

    /**
     * 编辑商品管理
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
			
            $result = $this->model->update($param);
            if( $result ) {
                xn_add_admin_log('编辑商品管理');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $_data = $this->model->find($id);
        $data = $_data->toArray();
        $data['status'] = $_data->getData('status');
		$type=Db::name('shoptype')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		$category=Db::name('shopcategory')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		$subject=Db::name('shopsubject')->where('status',1)->order('weigh asc,create_time desc')->select()->toArray();
		return view('form',compact('type','category','subject','data'));
    }

    /**
     * 删除商品管理
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $this->model->destroy($id);
        xn_add_admin_log('删除商品管理');
        $this->success('删除成功');
    }
}